package br.com.models;

import java.net.URL;
import java.util.Iterator;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.springframework.stereotype.Component;

@Component
@Entity
@Table(name="Address")
public class Address {
	
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	private Long id;
	
	@NotNull @Size(min=2,max=60)
	private String street;
	
	@NotNull
	private Integer home_number;
	
	@NotNull @Size(min=2,max=40)
	private String neighborhood;
	
	@NotNull @Size(min=2,max=20)
	private String complement;
	
	@NotNull @Size(min=10,max=10)
	private String cep;
	
	@NotNull @Size(min=2,max=40)
	private String city;
	
	@NotNull @Size(min=2,max=40)
	private String state;
	
	@NotNull @Size(min=2,max=40)
	private String country; // varchar(40) NOT NULL,	
	
	private int result;
	
	public Address() {
		
	}
	
	@SuppressWarnings("rawtypes")
	public void InicializeAdress(String cep){
		try {
			URL url = new URL("http://cep.republicavirtual.com.br/web_cep.php?cep=" + cep
                    + "&formato=xml");
			org.dom4j.Document document = getDocument(url);
			 
            Element root = document.getRootElement();
            
            for(Iterator i = root.elementIterator(); i.hasNext();){
            	Element element = (Element)i.next();
            	
            	if (element.getQualifiedName().equals("uf"))
                    setState(element.getText());
 
                if (element.getQualifiedName().equals("cidade")){
                    setCity(element.getText());
                    setCep(cep);
                }
 
                if (element.getQualifiedName().equals("bairro"))
                    setNeighborhood(element.getText());
 
                if (element.getQualifiedName().equals("logradouro"))
                    setStreet(element.getText());
 
                if (element.getQualifiedName().equals("resultado"))
                    setResult(Integer.parseInt(element.getText()));
            }
		} catch (Exception e) {
			
		}
	}
	
	public org.dom4j.Document getDocument(URL url) throws DocumentException{
		SAXReader reader = new SAXReader();
		org.dom4j.Document document = reader.read(url);
		return (org.dom4j.Document) document;
	}
	
	public int getResult() {
		return result;
	}

	public void setResult(int result) {
		this.result = result;
	}
	
	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getStreet() {
		return street;
	}
	public void setStreet(String street) {
		this.street = street;
	}
	public Integer getHome_number() {
		return home_number;
	}
	public void setHome_number(Integer home_number) {
		this.home_number = home_number;
	}
	public String getNeighborhood() {
		return neighborhood;
	}
	public void setNeighborhood(String neighborhood) {
		this.neighborhood = neighborhood;
	}
	public String getComplement() {
		return complement;
	}
	public void setComplement(String complement) {
		this.complement = complement;
	}
	public String getCep() {
		return cep;
	}
	public void setCep(String cep) {
		this.cep = cep;
	}
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	public String getState() {
		return state;
	}
	public void setState(String state) {
		this.state = state;
	}
	public String getCountry() {
		return country;
	}
	public void setCountry(String country) {
		this.country = country;
	}
}
